/**
 * @date 2005-12-12 9:40:28
 *
 */
package org.nanhill.commons.model.table;

import java.util.ArrayList;
import java.util.List;

/**
 * @author alin
 * 
 */
public class Row {
	private List<Cell> cells;

	public Row() {
		this.cells = new ArrayList<Cell>();
	}

	public Cell removeCell(int cellIndex) {
		return this.cells.remove(cellIndex);
	}
	
	public void setCell(int cellIndex, Cell cell) {
		this.cells.set(cellIndex, cell);
	}

	public Cell createCell(int cellIndex) {
		return createCell(cellIndex, null);
	}

	public Cell createCell(int cellIndex, String cellValue) {
		Cell cell;
		int size = this.cells.size();
		// if cell index is overflow cells
		if (cellIndex > size - 1) {
			// fill the cells with null
			for (int i = size; i < cellIndex; i++) {
				this.cells.add(null);
			}
			cell = new Cell(cellValue);
			this.cells.add(cell);
		} else {
			cell = this.cells.get(cellIndex);
			if (null == cell) {
				this.cells.remove(cellIndex);
				cell = new Cell(cellValue);
				this.cells.add(cellIndex, cell);
			}
		}
		return cell;
	}

	public Cell createCell(String cellValue) {
		Cell cell = new Cell(cellValue);
		this.cells.add(cell);
		return cell;
	}

	public Cell createCell() {
		return createCell(null);
	}
	
	public Cell getCell(int cellIndex) {
		return this.cells.get(cellIndex);
	}

	public List<Cell> getCells() {
		return cells;
	}

	public void setCells(List<Cell> cells) {
		this.cells = cells;
	}

}
